#include "key.h"

typedef enum
{
    USER_BUTTON_0 = 0,
    USER_BUTTON_1,
    USER_BUTTON_MAX
} user_button_t;

static flex_button_t user_button[USER_BUTTON_MAX];

void KEY_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    KEY_1_CLK();
    KEY_2_CLK();

    GPIO_InitStruct.Pin = KEY_1_PIN | KEY_2_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(KEY_1_GPIO, &GPIO_InitStruct);

    user_button_init(); // Initialize user buttons
}

static void btn_0_cb(flex_button_t *btn)
{
    printf("btn_0_cb\n");
    switch (btn->event)
    {
        case FLEX_BTN_PRESS_DOWN:
            printf("btn_0_cb [FLEX_BTN_PRESS_DOWN]\n");
            break;
        case FLEX_BTN_PRESS_CLICK:
            printf("btn_0_cb [FLEX_BTN_PRESS_CLICK]\n");
            //vTaskResume(xTask_showoled); // Resume OLED task
            break;
        case FLEX_BTN_PRESS_DOUBLE_CLICK:
            printf("btn_0_cb [FLEX_BTN_PRESS_DOUBLE_CLICK]\n");
            // vTaskResume(xTask_SR04); // Resume SR04 task
            // vTaskSuspend(xTask_showoled); // Suspend OLED task
            break;
        case FLEX_BTN_PRESS_SHORT_START:
            printf("btn_0_cb [FLEX_BTN_PRESS_SHORT_START]\n");
            break;
        case FLEX_BTN_PRESS_SHORT_UP:
            printf("btn_0_cb [FLEX_BTN_PRESS_SHORT_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_START:
            printf("btn_0_cb [FLEX_BTN_PRESS_LONG_START]\n");
            break;
        case FLEX_BTN_PRESS_LONG_UP:
            printf("btn_0_cb [FLEX_BTN_PRESS_LONG_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD:
            printf("btn_0_cb [FLEX_BTN_PRESS_LONG_HOLD]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD_UP:
            printf("btn_0_cb [FLEX_BTN_PRESS_LONG_HOLD_UP]\n");
            break;
    }
}

static void btn_1_cb(flex_button_t *btn)
{
    printf("btn_1_cb\n");
    switch (btn->event)
    {
        case FLEX_BTN_PRESS_DOWN:
            printf("btn_1_cb [FLEX_BTN_PRESS_DOWN]\n");
            break;
        case FLEX_BTN_PRESS_CLICK:
            printf("btn_1_cb [FLEX_BTN_PRESS_CLICK]\n");
            break;
        case FLEX_BTN_PRESS_DOUBLE_CLICK:
            printf("btn_1_cb [FLEX_BTN_PRESS_DOUBLE_CLICK]\n");
            // vTaskSuspend(xTask_showoled);
            // vTaskResume(xTask_DHT11); // Resume DHT11 task
            break;
        case FLEX_BTN_PRESS_SHORT_START:
            printf("btn_1_cb [FLEX_BTN_PRESS_SHORT_START]\n");
            break;
        case FLEX_BTN_PRESS_SHORT_UP:
            printf("btn_1_cb [FLEX_BTN_PRESS_SHORT_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_START:
            printf("btn_1_cb [FLEX_BTN_PRESS_LONG_START]\n");
            break;
        case FLEX_BTN_PRESS_LONG_UP:
            printf("btn_1_cb [FLEX_BTN_PRESS_LONG_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD:
            printf("btn_1_cb [FLEX_BTN_PRESS_LONG_HOLD]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD_UP:
            printf("btn_1_cb [FLEX_BTN_PRESS_LONG_HOLD_UP]\n");
            break;
    }
}

static uint8_t button_key0_read(void)
{
    return HAL_GPIO_ReadPin(KEY_1_GPIO, KEY_1_PIN) == GPIO_PIN_RESET ? 0 : 1;
}

static uint8_t button_key1_read(void)
{
    return HAL_GPIO_ReadPin(KEY_2_GPIO, KEY_2_PIN) == GPIO_PIN_RESET ? 0 : 1;
}

static void user_button_init(void)
{
    int i;
    
    memset(&user_button[0], 0x0, sizeof(user_button));

    user_button[USER_BUTTON_0].usr_button_read = button_key0_read;
    user_button[USER_BUTTON_0].cb = (flex_button_response_callback)btn_0_cb;

    user_button[USER_BUTTON_1].usr_button_read = button_key1_read;
    user_button[USER_BUTTON_1].cb = (flex_button_response_callback)btn_1_cb;

    for (i = 0; i < USER_BUTTON_MAX; i ++)
    {
        user_button[i].pressed_logic_level = 0;
        user_button[i].click_start_tick = 20;
        user_button[i].short_press_start_tick = 100;
        user_button[i].long_press_start_tick = 200;
        user_button[i].long_hold_start_tick = 300;

        flex_button_register(&user_button[i]);
    }
}


